//
//  UserDefaultManagement.m
//  Lvmm
//
//  Created by zhouyi on 13-3-28.
//  Copyright (c) 2013年 Lvmama. All rights reserved.
//

#import "UserDefaultManagement.h"

static UserDefaultManagement *userDefaultsInstance = nil;

@implementation UserDefaultManagement

+ (UserDefaultManagement *)instance {
	@synchronized(self) {
		if (userDefaultsInstance == nil) {
			userDefaultsInstance = [[self alloc] init];
            userDefaultsInstance.localDataDic = [NSMutableDictionary dictionary];
		}
	}
	return userDefaultsInstance;
}

- (id)init {
	if (self = [super init]) {
		self.defaults = [NSUserDefaults standardUserDefaults];
    }
    return self;
}

- (void)setUser:(User *)user {
    [self.defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:user] forKey:@"user"];
    [self.defaults synchronize];
}

- (User *)user {
    if ([self.defaults objectForKey:@"user"]) {
        id user = [NSKeyedUnarchiver unarchiveObjectWithData:[self.defaults objectForKey:@"user"]];
        if ([user isKindOfClass:CLASS(@"User")]) {
            return user;
        }
    }
    return nil;
}

- (void)setLocalDataDic:(NSMutableDictionary *)localDataDic {
    [self.defaults setObject:localDataDic ? : [NSMutableDictionary dictionary] forKey:@"localDataDic"];
    [self.defaults synchronize];
}

- (NSMutableDictionary *)localDataDic {
    if ([self.defaults objectForKey:@"localDataDic"]) {
        return [self.defaults objectForKey:@"localDataDic"];
    }
    return [NSMutableDictionary dictionary];
}

- (void)setShowZangYu:(BOOL)showZangYu {
    [self.defaults setObject:[NSNumber numberWithBool:showZangYu] forKey:@"zangyu"];
    [self.defaults synchronize];
    APP_DELEGATE.showZangYu = showZangYu;
}

- (BOOL)showZangYu {
    if ([self.defaults objectForKey:@"zangyu"]) {
        return [[self.defaults objectForKey:@"zangyu"] boolValue];
    }
    return NO;
}

- (void)saveLoginedUserID:(NSString *)userID {
    if (!userID) {
        return;
    }
    [self.defaults setObject:userID forKey:@"userID"];
}

- (NSString *)getLastLoginedUserID {
    return [self.defaults objectForKey:@"userID"];
}
@end
